// import store from '@/store'
// const token = store.state.value.user.userInfo
import { useUserStore } from '@/store/user'
import { whiteList } from './whitePage'

const loginPage = '/pages-sub/login/index'
export default function initPermission() {
  const routeFnList = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
  routeFnList.forEach((item) => {
    // debugger
    uni.addInterceptor(item, {
      invoke(e) {
        console.log('caonima')
        //这里判断用户有没有登录，如果项目较大，可使用store状态去判断，依个人情况
        const phoneNumber = useUserStore().userInfo.phoneNumber
        const url = e.url.split('?')[0]
        let notNeed = whiteList.includes(url)
        console.log(phoneNumber, notNeed, url)
        if (notNeed) {
          return e
        } else {
          if (!phoneNumber) {
            uni.showToast({
              title: '请登录',
              icon: 'none',
            })
            uni.navigateTo({
              url: loginPage,
            })
            return false
          } else {
            return e
          }
        }
      },
      success(res) {},
      fail(err) {},
    })
  })
}
